package net.trustie.core;
import net.trustie.utils.ExtractMutilLink4Openhub;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;
import java.util.regex.Matcher;

import java.util.List;

public class GithubRepoPageProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);


    private List<String> homepage;

    @Override
    public void process(Page page) {
         page.putField("links",page.getHtml().xpath("//div[@class='links center-block']/div[1]/div[2]/div/h5/a/@href"));
        homepage = page.getHtml().xpath("//div[@class='links center-block']/div[1]/div[2]/div/h5/a").links().all();
    }

    @Override
    public Site getSite() {
        return site;
    }
//    public void setHomepage(String homepage){
//        homepage = homepage;
//    }
    public  List<String> getHomepage(){
        return this.homepage;
    }

    public static String extractHomepage(String url){
        String homePage = "";
        GithubRepoPageProcessor githubRepoPageProcessor = new GithubRepoPageProcessor();
        Spider.create(githubRepoPageProcessor).addUrl("https://www.openhub.net/p/zimbra/links").thread(5).run();
        if(!githubRepoPageProcessor.getHomepage().isEmpty()){
            for(String one : githubRepoPageProcessor.getHomepage()){
                homePage += one+";";
            }
            homePage = homePage.substring(0,homePage.lastIndexOf(";"));
        }
        return homePage;
    }

    public static void main(String[] args) {
        GithubRepoPageProcessor githubRepoPageProcessor = new GithubRepoPageProcessor();
        String url = "/p/jQuery/links";
        Spider.create(githubRepoPageProcessor).addUrl(new String("https://www.openhub.net"+url)).thread(5).run();
        System.out.print(githubRepoPageProcessor.getHomepage().get(0));
//        String hompage = "/p/bac";
//        System.out.println(hompage.startsWith("/"));
        String result = new ExtractMutilLink4Openhub().extractLink("/p/jQuery/links");
        System.out.println(result);
    }
}
